Post

Replies

Boosts

Views

Activity

Reply to Using Generic SwiftData Modules
@DTS Engineer I think the code below works, ill keep testing and update you, import SwiftUI import SwiftData struct SwiftUIView<T: PersistentModel>: View { @Bindable var dataModule: T var keyPath: KeyPath<T, String> var body: some View { HStack(alignment: .top) { Text(dataModule[keyPath: keyPath]) } } } #Preview { do { let configuration = ModelConfiguration(isStoredInMemoryOnly: true) let container = try ModelContainer(for: Patient.self, configurations: configuration) let example = Patient(firstName: "First Name", mobileNumber: "+974 1234 5678", homePhone: "+974 1234 5678", email: "firstname.lastname@gmail.com") return SwiftUIView(dataModule: example, keyPath: \.firstName) .modelContainer(container) } catch { fatalError("Fatal Error") } }
2w
Reply to Using Generic SwiftData Modules
Ok now the solution you suggested raised few more questions and errors as below .. Now to use the T with text such as Text(T.age) it will complain that T doesn't have no age property ? Now suppose I want even the property of the T to be also generic and passed to the view as argument how we do it like T.T ? can I see an example ? Im trying to make a generic view that shows any property of any SwiftData module. Also #Preview is compiling that the generic variable isn't initialized and I wonder how can we initialize a generic variable ? I get the error .. Missing argument for parameter 'dataModule' in call import SwiftUI import SwiftData struct GenderList2<T: PersistentModel>: View { @Bindable var dataModule: T var body: some View { HStack(alignment: .top) { Text(T.age) } } } #Preview { GenderList2() }
2w
Reply to SwipeAction in For Each
Hello @Claude31 , thanks for the replay, below is the full code .. import SwiftUI import SwiftData struct PatientsList: View { @Environment(\.modelContext) var modelContext @Query(sort: [SortDescriptor(\Patient.firstName), SortDescriptor(\Patient.birthday)]) var patients: [Patient] var body: some View { List { ForEach(patients) { patient in HStack { NavigationLink (value: patient) { PatientRow(patient: patient) } } .alignmentGuide(.listRowSeparatorLeading) { _ in -50 } .listRowSeparatorTint(sysSecondary02) .swipeActions(edge: .trailing) { Button(role: .destructive) { deletePatient(patient: patient) } label: { VStack { Label("Delete", systemImage: "trash") Image(systemName: "Trash") } } } } } .scrollIndicators(.hidden) .foregroundStyle(sysSecondary08) .listStyle(.plain) } func deletePatient(patient: Patient) { modelContext.delete(patient) } init(sort: SortDescriptor<Patient>, searchString: String) { _patients = Query(filter: #Predicate { if searchString.isEmpty { return true } else { return $0.firstName.localizedStandardContains(searchString) } }, sort: [sort]) } } #Preview { PatientsList(sort: SortDescriptor(\Patient.firstName), searchString: "") }
Oct ’24
Reply to One to Many Relationship in SwiftData
I tried the code below, with SiftData models also below, but I got the error .. Value of type Sight has no member destination ! ForEach (destination.sights) { sight in VStack { Text(sight.name) Text(sight.destination.name) } } @Model class Destination { var name: String var details: String var date: Date var priority: Int @Relationship(deleteRule: .cascade) var sights = [Sight]() init(name: String = "", details: String = "", date: Date = .now, priority: Int = 2) { self.name = name self.details = details self.date = date self.priority = priority } } @Model class Sight { var name: String init(name: String) { self.name = name } }
Sep ’24
Reply to Text Don't Show in swipeActions buttons !
Thanks, it's working now, one last question is it possible to use views such as in code below ? maybe for example I want to show to circle buttons ? I remember Apple Mail used this design before ? Kind Regards Button(role: .destructive) { withAnimation { deletePatient2() } } label: { View1() } Button { deletePatient2() } label: { View2() } .tint(.orange) }
Jul ’24
Reply to Hiding chevron from list, SwiftUI
ForEach (patients) { patient in NavigationLink (value: patient) { PatientRow(patient: patient) } } .onDelete(perform: deletePatient) .listRowSeparator(.hidden) } .listStyle(.plain) .buttonStyle(PlainButtonStyle()) .scrollIndicators(.hidden) .navigationDestination(for: Patient.self) { patient in PatientDetails(patient: patient) }
Jun ’24